# -*- coding: utf-8 -*-

# Copyright (c) 2011, Sergey Karakovskiy, Nikolay Sohryakov
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Yasker nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import sys
import json
import logging
import threading
from util import storage
import core

from Keys import *

__author__ = 'Nikolay Sohryakov, Sergey Karakovskiy'
__copyright__ = 'Copyright 2011, Yasker'
__license__ = 'New BSD'
__email__ = 'Nikolay.Sohryakov+yasker@gmail.com, Sergey.Karakovskiy+yasker@gmail.com'


def response_dispatcher(message):
#	logging.debug('XMPP message received: %s' % message)
	yasker_response = json.loads(message)
	result = getattr(sys.modules[__name__], 'code_%s' % yasker_response[0])(
		yasker_response[1])
	core.events_queue.put(result)
	return result


def code_201(projects_list):
	"""GET ALL PROJECTS"""
	for project in projects_list:
		try:
			project_details_list = [project[PROJECT_ID], project[TITLE],
			                        project[DESCRIPTION]]
			storage.add_project(project_details_list)
		except: #TODO: specify exception here and in all methods below
			pass
	return core.Events.GET_ALL_PROJECTS, projects_list


def code_202(milestones_list):
	"""GET PROJECT MILESTONES"""
	for milestone in milestones_list:
		try:
			milestone_details_list = [milestone['p_id'], milestone['m_id'],
			                          milestone['title'],
			                          milestone['description']]
			storage.add_milestone(milestone_details_list)
		except:
			pass
	return core.Events.GET_PROJECT_MILESTONES, milestones_list


def code_216(response_details):
	"""GET MILESTONE TASKS"""
	pass


def code_203(response_details):
	"""GET UPDATES"""
	pass


def code_204(project_details):
	"""ADD PROJECT"""
	try:
		project_details_list = [project_details[PROJECT_ID],
		                        project_details[TITLE],
		                        project_details[DESCRIPTION],
		]
		storage.add_project(project_details_list)
	except:
		print sys.exc_info()
	return core.Events.ADD_PROJECT, project_details


def code_206(task_details):
	"""ADD TASK"""
	try:
		task_details_list = [task_details[PROJECT_ID],
		                     task_details[TASK_PARENT],
		                     task_details[TASK_ID],
		                     task_details[TITLE],
		                     task_details[DESCRIPTION],
		]
		storage.add_task(task_details_list)
	except:
		print sys.exc_info()
	return core.Events.ADD_TASK, task_details


def code_207(response_details):
	"""UPDATE PROJECT DETAILS"""
	pass


def code_208(response_details):
	"""UPDATE MILESTONE DETAILS"""
	pass


def code_209(response_details):
	"""UPDATE TASK"""
	pass


def code_210(response_details):
	"""DELETE PROJECT"""
	pass


def code_211(response_details):
	"""DELETE MILESTONE"""
	pass


def code_212(response_details):
	"""DELETE TASK"""
	pass
